package com.bgs.travelapp.card.service.impl;

import com.bgs.travelapp.card.entity.CardDetailsEntity;
import com.bgs.travelapp.card.entity.CardEntity;
import com.bgs.travelapp.card.entity.CardRecordEntity;
import com.bgs.travelapp.card.mapper.CardDetailsMapper;
import com.bgs.travelapp.card.mapper.CardMapper;
import com.bgs.travelapp.card.mapper.CardRecordMapper;
import com.bgs.travelapp.card.service.CardRecordService;
import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 购买会员卡记录管理   (前端--会员卡购买)
 * gpd 2022-07-20 09点12分
 */
@Service
public class CardRecordServiceImpl implements CardRecordService {

    @Resource
    CardRecordMapper cardRecordMapper;

    @Resource
    CardMapper cardMapper;

    @Resource
    CardDetailsMapper cardDetailsMapper;

    /**
     * 新增会员卡 （前端---用户购买会员卡功能）
     * gpd 2022-08-02 17:16
     * @param cardRecordEntity
     * @return
     */
    @Override
    public Integer saveCardRecord(CardRecordEntity cardRecordEntity) {
        List<CardRecordEntity> cardRecords = cardRecordMapper.selectexist(cardRecordEntity.getUserId(), cardRecordEntity.getExpiredId(), cardRecordEntity.getCardId());
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        if (!cardRecords.isEmpty()) {
            int month = 0;
            for (CardRecordEntity cardRecord1 : cardRecords) {
                cal.setTime(date);
                month += cardRecord1.getPurchaseMonth();
                cal.add(Calendar.MONTH, cardRecordEntity.getPurchaseMonth() + month);
            }
            cardRecordEntity.setExpiredId(0);
            cardRecordEntity.setCreateTime(date);
            cardRecordEntity.setExpirationTime(cal.getTime());
            return cardRecordMapper.insertSelective(cardRecordEntity);
        } else {
            cardRecordEntity.setCreateTime(date);
            cal.setTime(date);
            cal.add(Calendar.MONTH, cardRecordEntity.getPurchaseMonth());
            cardRecordEntity.setExpirationTime(cal.getTime());
            cardRecordEntity.setExpiredId(0);
            return cardRecordMapper.insertSelective(cardRecordEntity);
        }

    }

    /**
     * 查询选择的会员卡信息 （前端---用户购买会员卡功能）
     * gpd 2022-08-02 17:16
     * @return
     */
    @Override
    public List<CardEntity> getChoiceCard(Integer cardId) {
        CardEntity cardEntity = new CardEntity();
        cardEntity.setId(cardId);
        return cardMapper.select(cardEntity);
    }


    /**
     * 查询用户购买会员是否过期  （前端---用户购买会员卡功能）
     * gpd 2022-08-02 17:16
     * @param userId
     * @param cardId
     * @return
     */
    @Override
    public List<CardRecordEntity> getMemberOverdue(Integer userId, Integer cardId) {
        return cardRecordMapper.getMemberOverdue(userId, cardId);
    }

    /**
     * 查询已选择的会员卡详情  （前端---用户购买会员卡功能）
     * gpd 2022-08-02 17:16
     * @return
     */
    @Override
    public List<CardDetailsEntity> getCardDetails(Integer cardId) {
        CardDetailsEntity cardDetailsEntity = new CardDetailsEntity();
        cardDetailsEntity.setCardId(cardId);
        return cardDetailsMapper.select(cardDetailsEntity);
    }

    /**
     * 查询用户购买未过期会员卡的种类（cardId）
     * gpd 2022-08-02 19:16
     *
     * @param userId
     * @return
     */
    @Override
    public List<CardRecordEntity> getUserCard(Integer userId) {
        return cardRecordMapper.getUserCard(userId);
    }

    /**
     * 查询用户购买会员卡的过期时间（前端--我的会员卡）
     * gpd 2022-08-03 17点18分
     *
     * @param userId
     * @param cardId
     * @return
     */
    @Override
    public CardRecordEntity getCardExpiration(Integer userId, Integer cardId) {
        return cardRecordMapper.getCardExpiration(userId, cardId);
    }

    /**
     * 查询用户所有购买会员卡记录 分页查询+模糊查（起始购买时间）（结束购买时间）（前端---我的会员卡（支付记录））
     * gpd 2022-08-04 16点12分
     *
     * @param userId
     * @param pageRequest
     * @param createTime
     * @param createTime1
     * @return
     */
    @Override
    public PageResult getBuyRecord(Integer userId, PageRequest pageRequest, String createTime, String createTime1) {

        MyPageHelper.startPage(pageRequest);
        Example example = new Example(CardRecordEntity.class);
        example.createCriteria().andEqualTo("userId", userId).andBetween("createTime", createTime, createTime1);
        PageResult pageResult = MyPageHelper.pageInfo(cardRecordMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * 查询用户还未过期的会员卡记录 分页查询+模糊查（起始购买时间）（结束购买时间）（前端---我的会员卡（查看明细））
     * gpd 2022-08-04 20点25分
     *
     * @param userId
     * @param pageRequest
     * @param createTime
     * @param createTime1
     * @param cardId
     * @return
     */
    @Override
    public PageResult getBuyCardRecord(Integer userId, PageRequest pageRequest, String createTime, String createTime1, Integer cardId) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(CardRecordEntity.class);
        example.createCriteria().andEqualTo("userId",userId).andEqualTo("cardId",cardId).andBetween("createTime",createTime,createTime1);
        example.orderBy("id").desc();
        PageResult pageResult = MyPageHelper.pageInfo(cardRecordMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * 查询统计信息（前端---我的会员卡（查看明细--统计））
     * gpd 2022-08-05 16点15分
     * @param userId
     * @return
     */
    @Override
    public List<CardRecordEntity> getStatistics(Integer userId) {
        CardRecordEntity cardRecordEntity = new CardRecordEntity();
        cardRecordEntity.setUserId(userId);
        return cardRecordMapper.select(cardRecordEntity);
    }

    /**
     * 定时任务
     * todo 定时查询用户购买会员卡过期时间
     * gpd 2022-08-08 10点37分
     * @return
     */
    @Override
    public List<CardRecordEntity> getQueryOverdueTasks() {
        CardRecordEntity cardRecordEntity = new CardRecordEntity();
        return cardRecordMapper.select(cardRecordEntity);
    }

    /**
     * 定时任务
     * todo 定时查询用户购买会员卡过期时间
     * 修改是否过期id
     * gpd 2022-08-08 10点37分
     * @return
     */
    @Override
    public Integer UpdateOverdueTasks(Integer id) {
        CardRecordEntity cardRecordEntity = new CardRecordEntity();
        cardRecordEntity.setId(id);
        cardRecordEntity.setExpiredId(1);
        return cardRecordMapper.updateByPrimaryKeySelective(cardRecordEntity);
    }


}
